package com.czz.manage.controller.admin;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.czz.common.exception.ResultCodeEnum;
import com.czz.common.vo.R;
import com.czz.manage.entity.User;
import com.czz.manage.query.UserQuery;
import com.czz.manage.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName: UserAdminController
 * @Author Czz
 * @Date 2020/2/17
 * @Time 16:14
 **/
@RestController
@RequestMapping(value = "/admin/users")
public class UserAdminController {

    @Autowired
    UserService userService;

    /**
    * @Description 分页查询所有用户
    * @Param [page, limit, userQuery]
    * @Return com.czz.common.vo.R
    * @Author Czz
    * @Date 2020/2/17
    * @Time 16:37
    **/
    @GetMapping(value = "{page}/{limit}")
    public R getUserPageList(@PathVariable Long page, @PathVariable Long limit, UserQuery userQuery){
        if (page <= 0 || limit <= 0){
            return R.setResult(ResultCodeEnum.INPUT_ERROR);
        }
        Page<User> pageParam = new Page<>(page, limit);
        userService.getUserPageList(pageParam,userQuery);
        List<User> records = pageParam.getRecords();
        Long total = pageParam.getTotal();
        return R.ok().data("total", total).data("rows", records);
    }

    /**
    * @Description 修改用户状态
    * @Param [id, status]
    * @Return com.czz.common.vo.R
    * @Author Czz
    * @Date 2020/2/17
    * @Time 17:17
    **/
    @PutMapping(value = "/{id}/{status}")
    public R updateUserStatus(@PathVariable String id,@PathVariable Integer status){
        if (StringUtils.isEmpty(id) || StringUtils.isEmpty(status)){
            return R.setResult(ResultCodeEnum.INPUT_ERROR);
        }
        userService.updateUserStatus(id,status);
        return R.ok();
    }

    /**
    * @Description 根据id查询用户
    * @Param [id]
    * @Return com.czz.common.vo.R
    * @Author Czz
    * @Date 2020/2/17
    * @Time 17:33
    **/
    @GetMapping(value = "{id}")
    public R getUserById(@PathVariable String id){
        if (StringUtils.isEmpty(id)){
            return R.setResult(ResultCodeEnum.INPUT_ERROR);
        }
        User user = userService.getById(id);
        return R.ok().data("item",user);
    }

    /**
    * @Description 根据id修改用户
    * @Param [id, user]
    * @Return com.czz.common.vo.R
    * @Author Czz
    * @Date 2020/2/17
    * @Time 17:34
    **/
    @PutMapping(value = "{id}")
    public R updateUserById(@PathVariable String id,User user){
        if (StringUtils.isEmpty(id)){
            return R.setResult(ResultCodeEnum.INPUT_ERROR);
        }
        user.setId(id);
        userService.updateById(user);
        return R.ok();
    }
}
